<?php

namespace App\Service\Kms\Task\Common;

use App\Models\Common\FsMessageTemplateModel;
use App\Models\Kms\Task\DailyTaskLogsModel;
use App\Models\Kms\Task\TaskModel;
use App\Models\Kms\Task\TaskNodesModel;
use App\Models\Kms\Task\TaskTemplateNodesModel;
use App\Models\Kms\User\KmsUser;
use App\Remote\Feishu\FeishuMsgRemote;
use App\Remote\Rms\RmsShopRemote;
use Illuminate\View\Compilers\BladeCompiler;

class ChatDailyTask extends BaseTask
{
    function getTaskNode(): array
    {
        // TODO: Implement getTaskNode() method.
        return [
            TaskTemplateNodesModel::BUSINESS_TYPE_GENERATE => [
                "businessStatusEnum"       => [
                    TaskNodesModel::BUSINESS_STATUS_STAY     => "待生成",
                    TaskNodesModel::BUSINESS_STATUS_FINISHED => "已生成",
                    TaskNodesModel::BUSINESS_STATUS_REJECT   => ""
                ],
                "rejectBusinessStatusEnum" => []
            ],
            TaskTemplateNodesModel::BUSINESS_TYPE_SUBMIT   => [
                "businessStatusEnum"       => [
                    TaskNodesModel::BUSINESS_STATUS_STAY     => "待提交",
                    TaskNodesModel::BUSINESS_STATUS_FINISHED => "已提交",
                    TaskNodesModel::BUSINESS_STATUS_REJECT   => ""
                ],
                "rejectBusinessStatusEnum" => [
                    TaskNodesModel::BUSINESS_STATUS_STAY     => "待修改",
                    TaskNodesModel::BUSINESS_STATUS_FINISHED => "已提交",
                    TaskNodesModel::BUSINESS_STATUS_REJECT   => ""
                ]
            ],
            TaskTemplateNodesModel::BUSINESS_TYPE_APPROVE  => [
                "businessStatusEnum"       => [
                    TaskNodesModel::BUSINESS_STATUS_STAY     => "待审批",
                    TaskNodesModel::BUSINESS_STATUS_FINISHED => "已确认完成",
                    TaskNodesModel::BUSINESS_STATUS_REJECT   => "已驳回"
                ],
                "rejectBusinessStatusEnum" => []
            ],
            TaskTemplateNodesModel::BUSINESS_TYPE_FINISHED => [
                "businessStatusEnum"       => [
                    TaskNodesModel::BUSINESS_STATUS_STAY     => "待完成",
                    TaskNodesModel::BUSINESS_STATUS_FINISHED => "已完成",
                    TaskNodesModel::BUSINESS_STATUS_REJECT   => ""
                ],
                "rejectBusinessStatusEnum" => []
            ]
        ];
    }


    function handleTaskNodeFinishEvent(TaskModel $taskModel)
    {
        // TODO: Implement handleTaskNodeFinishEvent() method.
        $templateModel = FsMessageTemplateModel::query()
                                               ->where("templateName", "KMS_DAILY_TASK")
                                               ->first();

        $businessModel = $taskModel->dailyTaskLogsRel;
        // todo 获取任务涉及用户信息
        $userModel = KmsUser::query()->whereIn(
            "rmsUserId",
            array_merge(
                [$businessModel->handlerUserId, $businessModel->createdByUid],
                jsonToArr($businessModel->approveUserIds, [])
            )
        )->get();

        // todo 获取门店信息
        $shopInfo = RmsShopRemote::getShopDetailByShopId($businessModel->shopId);

        // todo 当前节点信息
        $nowTaskNode = $taskModel->taskNodesRel->first();
        $url         = config('webUrl.notify_web_cdn_url') . "#/pages/operate/task/detail?id=" . $businessModel->id . "&from=fs";
        $webViewLink = "kuafoodkmsapp://webView?url=" . urlencode($url);

        if (in_array($nowTaskNode->step, [0, 1, 2, 3])) {
            $fsRmsUserIds = null;
            switch ($nowTaskNode->step) {
                case 1://任务生成
                    $taskStatusText = "客服 新任务";
                    $fsRmsUserIds   = array_merge(
                        jsonToArr($businessModel->approveUserIds, []),
                        [$businessModel->handlerUserId]
                    );
                    break;
                case 3://审批
                    if ($nowTaskNode->businessStatus == TaskNodesModel::BUSINESS_STATUS_REJECT) {
                        $taskStatusText = "已驳回";
                    } else {
                        $taskStatusText = "已通过";
                    }
                    $fsRmsUserIds = [$businessModel->handlerUserId];
                    break;
                case 0://作废
                    $taskStatusText = "已作废";
                    $fsRmsUserIds   = [$businessModel->handlerUserId];
                    break;
                case 2://处理人提交表单完成 发送待审批消息
                    $taskStatusText = "待审批";
                    $fsRmsUserIds   = jsonToArr($businessModel->approveUserIds, []);
                    break;
            }
            if ($fsRmsUserIds) {
                $fsUserIds = $userModel->whereIn("rmsUserId", $fsRmsUserIds)
                                       ->pluck("userId")
                                       ->toArray();
                if ($fsUserIds) {
                    FeishuMsgRemote::getInstance()->batchSendInteractiveMsg(
                        BladeCompiler::render(
                            $templateModel->templateContent, [
                            "shopName"       => attributeGet($shopInfo, "shopDetail.name", ""),
                            "createUsername" => self::getUsername($businessModel->createdByUid, $userModel),
                            "handleUsername" => self::getUsername($businessModel->handlerUserId, $userModel),
                            "taskStatusText" => $taskStatusText,
                            "taskTypeText"   => DailyTaskLogsModel::TASK_TYPE_TEXT[$businessModel->taskType] ?? "-",
                            "createTime"     => $businessModel->createdAt->format("Y-m-d H:i:s"),
                            "url"            => $webViewLink
                        ]),
                        $fsUserIds
                    );
                }
            }

        }
    }

}
